/**  
* @Title: WechatBankCardServiceImpl.java
* @Package com.hjb.service.impl.Wechat
* @author <a href="mailto:125671254@qq.com">Pan Xinwen</a>panxinwen
* @date 2018年1月13日 下午12:46:12
* @version V1.0  
*/ 
package com.hjb.service.impl.Wechat;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.hjb.service.Wechat.WechatBankCardService;
import com.hjb.service.annotation.SiteUserBankCard;
import com.hjb.service.com.ComVerifyCodeService;
import com.hjb.service.domain.commsglog.ComMsgLog;
import com.hjb.service.domain.siteuser.SiteUser;
import com.hjb.service.domain.siteuseraccount.SiteUserBankcard;
import com.hjb.service.domain.siteuseraccount.SiteUserBankcardExample;
import com.hjb.service.gateway.baofoo.BaoFooPayService;
import com.hjb.service.mapper.SiteUserBankcardMapper;
import com.hjb.service.mapper.SiteUserMapper;

/**
* @ClassName: WechatBankCardServiceImpl
* @Description: TODO(用户银行卡信息维护)
* @author <a href="mailto:125671254@qq.com">Pan Xinwen</a>panxinwen
* @date 2018年1月13日 下午12:46:12
*
*/
@Service
public class WechatBankCardServiceImpl implements WechatBankCardService {

	@Autowired
	private SiteUserBankcardMapper siteUserBankcardMapper;
	@Autowired
	private SiteUserMapper siteUserMapper;
	@Autowired
	private BaoFooPayService baoFooPayService;
	@Autowired
	private ComVerifyCodeService comVerifyCodeService;

	
	@Override
	@Transactional(rollbackFor = Exception.class)
	public Map<String,String> bindingBankCard(SiteUserBankcard siteUserBankcard) {
		SiteUserBankcardExample example = new SiteUserBankcardExample();
		example.createCriteria().andCardNoEqualTo(siteUserBankcard.getCardNo());
		//查询银行卡是否绑定
		List<SiteUserBankcard> list = siteUserBankcardMapper.selectByExample(example);
		
		Map<String,String> resMap = new HashMap<String,String>();
		
		resMap.put("code",SiteUserBankCard.BANK_CARD_BASE+"");
		resMap.put("msg", SiteUserBankCard.BANK_CARD_BASE.MSG());
		
		if(list != null && list.size() >0){
			//银行卡已绑定
			resMap.put("code",SiteUserBankCard.BANK_CARD_BIND+"");
			resMap.put("msg", SiteUserBankCard.BANK_CARD_BIND.MSG());
		}else{
			siteUserBankcard.setSerialNo("JXBK"+System.currentTimeMillis());
			//查询用户信息
			SiteUser siteUser = siteUserMapper.selectByPrimaryKey(siteUserBankcard.getUserId());
			siteUser.setBankCardNo(siteUserBankcard.getCardNo());
			siteUser.setBankPhone(siteUserBankcard.getRemainPhone());

			
			Map<String,String> param = new HashMap<String,String>();
			
			
			
			param.put("acc_no", siteUserBankcard.getCardNo());
			param.put("trans_id", "JXBK"+System.currentTimeMillis());
			param.put("id_card", siteUser.getCardNumber());
			param.put("id_holder", siteUser.getName());
			param.put("mobile", siteUserBankcard.getRemainPhone());
			param.put("pay_code", siteUserBankcard.getBankCode());
			
			//调用宝付鉴权
			Map<String, Object> returned = baoFooPayService.bindCard(param);
			
			if("0000".equals(returned.get("resp_code"))){
				//业务绑定
				siteUserBankcard.setOrderId(returned.get("bind_id").toString());
				siteUserBankcardMapper.insert(siteUserBankcard);
				siteUser.setBindId(returned.get("bind_id").toString());
				//更新用户信息
				siteUserMapper.updateByPrimaryKeySelective(siteUser);
				
			}else {
				resMap.put("code",SiteUserBankCard.BANK_CARD_BIND_FAIL+"");
				resMap.put("msg", returned.get("resp_msg")+"");
			}
			
		}
		
		return resMap;
		
	}

	@Override
	public Object getSmsCode(String remainPhone) {
		
		ComMsgLog comMsgLog = new ComMsgLog();
		
		comMsgLog.setPhoneNum(remainPhone);
		
		return comVerifyCodeService.sendShorMsgVerifyCode(comMsgLog,2);
	}

}
